package cn.com.cifi.mars.web.action.sync;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.constants.VersionConstant;
import cn.com.cifi.mars.common.enums.LocalCommandEnum;
import cn.com.cifi.mars.common.enums.RedisChannelEnum;
import cn.com.cifi.mars.constant.StageStatusEnum;
import cn.com.cifi.mars.schedled.SyncRemovalReportTasks;
import cn.com.cifi.mars.service.CacheService;
import cn.com.cifi.mars.service.CommonService;
import cn.com.cifi.mars.service.DmDmRemovalReportService;
import cn.com.cifi.mars.service.DmDmValueReportService;
import cn.com.cifi.mars.service.DmOdsMyVsMarsSContractService;
import cn.com.cifi.mars.service.DmOdsNosTradedataviewformarsService;
import cn.com.cifi.mars.util.DateUtils;
import cn.com.cifi.mars.util.StrUtils;

/**
 * 
 * @author plx
 *
 */
@RestController
public class ReportSyncController extends BaseAction implements IReportSyncController {

    @Autowired
    private CacheService  cacheService;

    @Autowired
    private DmOdsMyVsMarsSContractService dmOdsMyVsMarsSContractService;

    @Autowired
    private DmOdsNosTradedataviewformarsService dmOdsNosTradedataviewformarsService;

    @Autowired
    private DmDmValueReportService dmDmValueReportService;

    @Autowired
    private DmDmRemovalReportService dmDmRemovalReportService;

    @Autowired
    private SyncRemovalReportTasks syncRemovalReportTasks;
    @Autowired
	private CommonService commonService;
    
    @Override
    public R<String> syncBase(String apiVersion,String yearMonth,String containsInactive) {
        if(StrUtils.isEmpty(yearMonth)) {
        	yearMonth = LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR));
        }
        if (StringUtils.isNotBlank(containsInactive)){
            dmDmValueReportService.sync(yearMonth,StageStatusEnum.CTIVE.getKey(),StageStatusEnum.INACTIVE.getKey());
        }else {
            dmDmValueReportService.sync(yearMonth,StageStatusEnum.CTIVE.getKey());
        }
        return ok();
    }

    @Override
    public R<String> syncMyContract(String apiVersion) {
        if (StringUtils.isNotBlank(apiVersion)) {
            switch (apiVersion) {
                case VersionConstant.V1:
                    dmOdsMyVsMarsSContractService.syncMyContract();
                    break;
                case VersionConstant.V2:
                    dmOdsMyVsMarsSContractService.syncIncrement();
                    break;
                default:
            }
        }
        return ok();
    }

    @Override
    public R<String> syncNosContact(String apiVersion) {
    	dmOdsNosTradedataviewformarsService.syncNosContact();
        return ok();
    }

    @Override
    public R<String> syncRemoval(String apiVersion, String yearMonth) {
        if(StrUtils.isEmpty(yearMonth)) {
        	yearMonth = LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR));
        }
        dmDmRemovalReportService.sync(LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR)));
        
        dmDmRemovalReportService.syncRemovalDataJob(yearMonth);
        return ok();
    }

    @Override
    public R<String> syncRemovalCache(String apiVersion) {
        if (VersionConstant.V1.equals(apiVersion)){
            cacheService.pushCommand(RedisChannelEnum.CHANNEL_REMOVAL,LocalCommandEnum.FLUSH_REMOVAL_REPORT_CACHE);
        }else if (VersionConstant.V2.equals(apiVersion)){
            cacheService.pushCommand(RedisChannelEnum.CHANNEL_REMOVAL,LocalCommandEnum.CLEAR_REMOVAL_REPORT_CACHE);
        }
        R ok = ok();
        ok.setMessage("已提交刷新命令");
        return ok;
    }
    
	@Override
	public R<String> syncRemovalJob(String apiVersion, String yearMonth) {
		syncRemovalReportTasks.syncDo(yearMonth);
		return ok();
	}

	@Override
	public R<String> syncRemovalDataJob(String apiVersion, String yearMonth) {
		dmDmRemovalReportService.syncRemovalDataJob(yearMonth);
		return ok();
	}
}
